package com.simple.card.controller;

import com.simple.card.Utils.AesUtil;
import com.simple.card.Utils.ResultVOUtil;
import com.simple.card.VO.ResultUserInfoVO;
import com.simple.card.VO.ResultVO;
import com.simple.card.annotation.LoginCheck;
import com.simple.card.annotation.SignCheck;
import com.simple.card.dto.CreditListDTO;
import com.simple.card.dto.KaBaoUserDTO;
import com.simple.card.dto.MyCardBasicDTO;
import com.simple.card.dto.TBankbillShoppingsheetDTO;
import com.simple.card.enums.ResultEnum;
import com.simple.card.exception.CreditException;
import com.simple.card.form.ConsumeDetailForm;
import com.simple.card.service.CreditCardService;
import com.simple.card.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/card")
@Slf4j
public class CreditCardController {

    @Autowired
    private CreditCardService creditCardService;

    @Autowired
    private UserService userService;

    /**
     * 获取卡包主页数据
     * @param
     * @return
     */
    @GetMapping(value = "/list")
    @SignCheck(sign = false)
    @LoginCheck(login = true)
    public ResultVO list(HttpServletRequest request) {
        String token = AesUtil.decryptData(request.getHeader("Access-User-Token"));
        KaBaoUserDTO kaBaoUserDTO = userService.findDTOByToken(token);
        //获取用户信用卡列表数据
        String userId = String.valueOf(kaBaoUserDTO.getUid());
        List<CreditListDTO> tCardList = creditCardService.findByUserId(userId);
        ResultUserInfoVO resultUserInfoVO = new ResultUserInfoVO();
        resultUserInfoVO.setKaBaoUserDTO(kaBaoUserDTO);
        resultUserInfoVO.setData(tCardList);
        return ResultVOUtil.success(resultUserInfoVO);
    }

    @PostMapping(value = "/consumedetail")
    @SignCheck(sign = false)
    @LoginCheck(login = true)
    public ResultVO consumeDetail(@Valid ConsumeDetailForm consumeDetailForm,
                                  BindingResult bindingResult,
                                  HttpServletRequest request) {
        if(bindingResult.hasErrors()) {
            log.error("[用户登录] 参数不正确,userLoginForm={}", consumeDetailForm.toString());
            throw new CreditException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        //获取token
        String token = AesUtil.decryptData(request.getHeader("Access-User-Token"));
        //获取用户信息
        KaBaoUserDTO kaBaoUserDTO = userService.findDTOByToken(token);
        List<TBankbillShoppingsheetDTO> tBankbillShoppingsheetDTOList = creditCardService.findByBillId(consumeDetailForm);
        ResultUserInfoVO resultUserInfoVO = new ResultUserInfoVO();
        resultUserInfoVO.setKaBaoUserDTO(kaBaoUserDTO);
        resultUserInfoVO.setData(tBankbillShoppingsheetDTOList);
        return ResultVOUtil.success(resultUserInfoVO);
    }

    @GetMapping(value = "/mycards")
    @SignCheck(sign = false)
    @LoginCheck(login = true)
    public ResultVO myCard(HttpServletRequest request) {
        String token = AesUtil.decryptData(request.getHeader("Access-User-Token"));
        KaBaoUserDTO kaBaoUserDTO = userService.findDTOByToken(token);
        String userId = String.valueOf(kaBaoUserDTO.getUid());
        //获取个人卡片
        List<MyCardBasicDTO> myCardBasicDTOList = creditCardService.findMyCardBasicList(userId);
        ResultUserInfoVO resultUserInfoVO = new ResultUserInfoVO();
        resultUserInfoVO.setKaBaoUserDTO(kaBaoUserDTO);
        resultUserInfoVO.setData(myCardBasicDTOList);
        return ResultVOUtil.success(resultUserInfoVO);
    }
}